<template>
  <j-modal
    :title="title"
    :width="800"
    :visible="visible"
    :confirmLoading="confirmLoading"
    switchFullscreen
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭">

    <a-spin :spinning="confirmLoading">
      <a-form :form="form">

        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="疾病名称">
          <a-input placeholder="请输入疾病名称"
                   v-decorator="['diseaseName', {rules: [{ required: true, message: '请输入疾病名称!' }]}]"/>
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="排序">
          <a-input-number v-decorator="[ 'sort', {rules: [{ required: true, message: '请输入疾病排序!' }]}]"/>
        </a-form-item>
        <a-form-item label="关联器官" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-select
            show-search
            option-filter-prop="children"
            :filter-option="filterOption"
            mode="multiple"
            v-model="partsOrganInfoList"
            style="width: 100%"
            placeholder="请选择关联器官"
            @change="handleChange">
            <a-select-option v-for="item in partsOrganList"
                             :value="item.id"
                             :key="item.id">
              {{item.organName}}
            </a-select-option>
          </a-select>
        </a-form-item>

      </a-form>
    </a-spin>
  </j-modal>
</template>

<script>
  import {getAction, httpAction} from '@/api/manage'
  import pick from 'lodash.pick'

  export default {
    name: "MedicalOrganDiseaseInfoModal",
    data() {
      return {
        title: "操作",
        visible: false,
        model: {},
        labelCol: {
          xs: {span: 24},
          sm: {span: 5},
        },
        wrapperCol: {
          xs: {span: 24},
          sm: {span: 16},
        },
        partsOrganList: [],
        partsOrganValueList: [],
        partsOrganInfoList: [],
        confirmLoading: false,
        form: this.$form.createForm(this),
        validatorRules: {},
        url: {
          add: "/recovery/medicalOrganDiseaseInfo/add",
          edit: "/recovery/medicalOrganDiseaseInfo/edit",
          getPartsOrganList: "/recovery/medicalPartsOrganInfo/getPartsOrganList",
        },
      }
    },
    created() {
      this.getPartsOrganList();
    },
    methods: {
      add() {
        this.edit({});
      },
      edit(record) {
        this.form.resetFields();
        this.model = Object.assign({}, record);
        this.visible = true;
        this.partsOrganInfoList = [];
        if (this.model.partsOrganInfoList && this.model.partsOrganInfoList.length > 0) {
          this.partsOrganInfoList = this.model.partsOrganInfoList;
        }
        this.$nextTick(() => {
          this.form.setFieldsValue(pick(this.model, 'diseaseName', 'sort', 'delFlag', 'partsOrganInfoList'))
          //时间格式化
        });
      },
      close() {
        this.$emit('close');
        this.visible = false;
      },
      handleOk() {
        const that = this;
        // 触发表单验证
        this.form.validateFields((err, values) => {
          if (!err) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if (!this.model.id) {
              httpurl += this.url.add;
              method = 'post';
            } else {
              httpurl += this.url.edit;
              method = 'put';
            }
            this.model.partsOrganInfoList = this.partsOrganInfoList;
            let formData = Object.assign(this.model, values);
            //时间格式化
            console.log(formData)
            httpAction(httpurl, formData, method).then((res) => {
              if (res.success) {
                that.$message.success(res.message);
                that.$emit('ok');
              } else {
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
              that.close();
            })
          }
        })
      },
      handleCancel() {
        this.close()
      },
      getPartsOrganList() {
        getAction(this.url.getPartsOrganList).then((res) => {
          if (res.success) {
            this.partsOrganList = res.result;
            console.info("打印参数：{}", this.partsOrganList)
          }
        });
      },
      handleChange(value) {
        this.partsOrganValueList = value;
      },
      filterOption(input, option) {
        return (
          option.componentOptions.children[0].text.toLowerCase().indexOf(input.toLowerCase()) >= 0
        );
      },
    }
  }
</script>

<style lang="less" scoped>

</style>